const {ipcMain} = require('electron');

let cp = require('child_process');
let path = require('path');

exports.eventSelectKeywordCover = function () {
    ipcMain.on('SelectKeywordCover', (event, data) => {
        console.log('data -->', data);
        let index = data.data.index;
        let keyword = data.data.keyword;
        let searchEngine = data.data.search_engine;
        let fugaiTiaoJian = data.fugaiTiaoJian;
        let eventName = data.eventName;
        let currentKeywordsIndex = data.currentKeywordsIndex;

        if (searchEngine === '百度') {
            // baiduPcSelectRank(keyword);
        } else if (searchEngine === '手机百度') {
            console.log('path.basename() -->', path.dirname(__filename));
            let childJsPath = path.join(path.dirname(__filename), 'keywords_cover/keywords_cover.js');
            let child = cp.fork(childJsPath);

            child.on('message', function (data) {
                console.log('主进程接收数据 -->', data);
                data.currentKeywordsIndex = currentKeywordsIndex;
                event.sender.send(eventName, data);
            });
            let sendData = {
                func: 'baiduWapSelectRank',
                index: index,
                keyword: keyword,
                fugaiTiaojian: fugaiTiaoJian,
                currentKeywordsIndex: currentKeywordsIndex
            };
            child.send(sendData);
            // baiduWapSelectRank(index, keyword, fugaiTiaojian, event);
        }
    });
};
